package com.shuqi.activity.viewport;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shuqi.activity.BookSearchParentActivity;
import com.shuqi.activity.BookSquareWebActivity;
import com.shuqi.activity.MainActivityGroup;
import com.shuqi.activity.RankWebActivity;
import com.shuqi.activity.SortWebActivity;
import com.shuqi.controller.R;
import com.shuqi.statistics.StatisticsEventID;
import com.shuqi.statistics.StatisticsUtils;

/**
 * 主页底部菜单
 * 
 * @author zhangpeng
 * 
 */
public class MainGroupBottomNav extends LinearLayout implements OnClickListener {
    private int currentPage = 0;
    private MainActivityGroup mContext;
    private LinearLayout mContent;
    private Class<?>[] classes = {BookSearchParentActivity.class, BookSquareWebActivity.class,
            RankWebActivity.class, SortWebActivity.class};


    private LinearLayout mainbottom_item1_frame;
    private LinearLayout mainbottom_item2_frame;
    private LinearLayout mainbottom_item3_frame;
    private LinearLayout mainbottom_item4_frame;

    public MainGroupBottomNav(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = (MainActivityGroup) context;
        View view = LayoutInflater.from(context).inflate(R.layout.view_mainbottom, null);
        addView(view, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        mainbottom_item1_frame = (LinearLayout) view.findViewById(R.id.mainbottom_item1_frame);
        mainbottom_item1_frame.setOnClickListener(this);
        mainbottom_item2_frame = (LinearLayout) view.findViewById(R.id.mainbottom_item2_frame);
        mainbottom_item2_frame.setOnClickListener(this);
        mainbottom_item3_frame = (LinearLayout) view.findViewById(R.id.mainbottom_item3_frame);
        mainbottom_item3_frame.setOnClickListener(this);
        mainbottom_item4_frame = (LinearLayout) view.findViewById(R.id.mainbottom_item4_frame);
        mainbottom_item4_frame.setOnClickListener(this);
    }

    /**
     * 设置当前页
     * 
     * @param cur
     */
    public void setCurrentPage(int cur) {
        currentPage = cur;
        setPage();
    }

    /**
     * 得到当前页
     * 
     * @return
     */
    public int getCurrentPage() {
        return currentPage;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.mainbottom_item1_frame:
                currentPage = 0;
                StatisticsUtils.onEvent(mContext, StatisticsEventID.BOOKSEARCH_SEARCH);
                break;
            case R.id.mainbottom_item2_frame:
                currentPage = 1;
                StatisticsUtils.onEvent(mContext, StatisticsEventID.BOOKSQUARE_RECOMMEND);
                break;
            case R.id.mainbottom_item3_frame:
                currentPage = 2;
                StatisticsUtils.onEvent(mContext, StatisticsEventID.BOOKSQUARE_RANKLIST);
                break;
            case R.id.mainbottom_item4_frame:
                currentPage = 3;
                StatisticsUtils.onEvent(mContext, StatisticsEventID.BOOKSQUARE_RANKLIST_SORT);
                break;

            default:
                break;
        }
        setPage();
    }

    /**
     * 设置子页面
     */
    private void setPage() {
        if (mContent == null) {
            mContent = (LinearLayout) mContext.findViewById(R.id.act_maingroup_content_lin);
        }
        Intent intent = new Intent(mContext, classes[currentPage]);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        View subActivity = null;
        try {
            subActivity =
                    mContext.getLocalActivityManager()
                            .startActivity("subActivity" + currentPage, intent).getDecorView();

            // 清除焦点，避免4.0有时候切换activity导致listview无法点击问题
            View v1 = mContent.getChildAt(0);
            if (v1 != null) {
                v1.clearFocus();
            }
            mContent.removeAllViews();
            mContent.removeView(subActivity);
            mContent.addView(subActivity, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
            subActivity.requestFocus();
        } catch (Exception e) {// .getDecorView();在2.2系统中是空指针，removeall或者cleartop能够解决问题
            e.printStackTrace();
            mContext.getLocalActivityManager().removeAllActivities();
            intent = new Intent(mContext, classes[currentPage]);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            subActivity =
                    mContext.getLocalActivityManager()
                            .startActivity("subActivity" + currentPage, intent).getDecorView();
            mContent.removeAllViews();
            mContent.addView(subActivity, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        }
        playAnim(currentPage);
    }

    /**
     * 设置选中状态
     * 
     * @param position
     */
    private void playAnim(int position) {
        TextView mainbottom_item1_tv = (TextView) findViewById(R.id.mainbottom_item1_tv);
        TextView mainbottom_item2_tv = (TextView) findViewById(R.id.mainbottom_item2_tv);
        TextView mainbottom_item3_tv = (TextView) findViewById(R.id.mainbottom_item3_tv);
        TextView mainbottom_item4_tv = (TextView) findViewById(R.id.mainbottom_item4_tv);


        ImageView mainbottom_item1_iv = (ImageView) findViewById(R.id.mainbottom_item1_iv);
        ImageView mainbottom_item2_iv = (ImageView) findViewById(R.id.mainbottom_item2_iv);
        ImageView mainbottom_item3_iv = (ImageView) findViewById(R.id.mainbottom_item3_iv);
        ImageView mainbottom_item4_iv = (ImageView) findViewById(R.id.mainbottom_item4_iv);


        mainbottom_item1_tv.setTextColor(getResources().getColor(R.color.mainboottom_tv_d));
        mainbottom_item1_iv.setImageDrawable(getResources().getDrawable(
                R.drawable.icon_mainbottom_item1_d));

        mainbottom_item2_tv.setTextColor(getResources().getColor(R.color.mainboottom_tv_d));
        mainbottom_item2_iv.setImageDrawable(getResources().getDrawable(
                R.drawable.icon_mainbottom_item2_d));

        mainbottom_item3_tv.setTextColor(getResources().getColor(R.color.mainboottom_tv_d));
        mainbottom_item3_iv.setImageDrawable(getResources().getDrawable(
                R.drawable.icon_mainbottom_item3_d));

        mainbottom_item4_tv.setTextColor(getResources().getColor(R.color.mainboottom_tv_d));
        mainbottom_item4_iv.setImageDrawable(getResources().getDrawable(
                R.drawable.icon_mainbottom_item4_d));
        switch (position) {
            case 0:
                mainbottom_item1_tv.setTextColor(getResources().getColor(R.color.mainboottom_tv_p));
                mainbottom_item1_iv.setImageDrawable(getResources().getDrawable(
                        R.drawable.icon_mainbottom_item1_p));
                break;
            case 1:
                mainbottom_item2_tv.setTextColor(getResources().getColor(R.color.mainboottom_tv_p));
                mainbottom_item2_iv.setImageDrawable(getResources().getDrawable(
                        R.drawable.icon_mainbottom_item2_p));
                break;
            case 2:
                mainbottom_item3_tv.setTextColor(getResources().getColor(R.color.mainboottom_tv_p));
                mainbottom_item3_iv.setImageDrawable(getResources().getDrawable(
                        R.drawable.icon_mainbottom_item3_p));
                break;
            case 3:
                mainbottom_item4_tv.setTextColor(getResources().getColor(R.color.mainboottom_tv_p));
                mainbottom_item4_iv.setImageDrawable(getResources().getDrawable(
                        R.drawable.icon_mainbottom_item4_p));
                break;
            default:
                break;
        }
    }

}
